package com.lxp.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lxp.service.MqttGateway;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.RoundingMode;

@Api(tags = "测试管理")
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private MqttGateway mqttGateway;

    @ApiOperation("测试")
    @GetMapping("/sendMqtt")
    public String sendMqtt(String topic) {
        double aDouble = RandomUtil.randomDouble(0.0, 500.0, 2, RoundingMode.DOWN);
        double v = RandomUtil.randomDouble(0.0, 100.0, 2, RoundingMode.DOWN);
        log.info("返回的{},湿度:{}", aDouble, v);
        JSONObject json1 = JSONUtil.createObj().set("method", aDouble).set("shidu", v);
        mqttGateway.sendToMqtt(json1.toString(), topic);
        return "OK";
    }
}
